import request from '@/utils/request'
import BaseApi from '../BaseApi'
import { encrypt } from '@/utils/jsencrypt'
import sessionUtil from '@/utils/session'
import router from '@/router'
import { useOptionDataStore } from '@/stores/optionData'
import { usePageTabStore } from '@/stores/pageTab'

/**
 * 用户模块
 */
const apiRouter = '/FaSysMemberApi'
export default class FaSysMember extends BaseApi {
  constructor(gridId) {
    super(apiRouter, gridId)
  }

  //登录加密
  loginEncrypt(data) {
    return {
      account: encrypt(data.account),
      password: encrypt(data.password)
    }
  }

  login(data) {
    // 登录信息加密
    const logonInfo = this.loginEncrypt(data)
    return request({
      url: `${apiRouter}/login`,
      method: 'post',
      data: logonInfo
    })
  }
  logout() {
    request({
      url: `${apiRouter}/logout`,
      method: 'get'
    }).then((res) => {
      this.logoutAfert()
    })
  }
  logoutAfert() {
    // 删除缓存
    sessionUtil.delAll()
    useOptionDataStore().clear()
    usePageTabStore().clear()
    // 路由跳转到登录页
    router.push('/')
  }

  changePassword(data) {
    return request({
      url: `${apiRouter}/changePassword`,
      method: 'post',
      data
    })
  }
}
